package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.zxp.R;

import java.util.Collections;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class GoodsDetailEditAdapter extends ItemAdapter<String, GoodsDetailEditAdapter.DefaultHolder> implements ItemTouchListener {


    private Context context;
    private int selectPoi = -1;

    public GoodsDetailEditAdapter(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_good_detail_edit, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        String item = getItem(position);
//        holder.poi = position;
        Glide.with(context).load(item).asBitmap().placeholder(R.color.image_default).into(holder.imgGoods);
        if (selectPoi == position) {
            holder.layoutEdit.setVisibility(View.VISIBLE);
        } else {
            holder.layoutEdit.setVisibility(View.GONE);
        }

        holder.imgGoods.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                selectPoi = position;
                holder.layoutEdit.setVisibility(View.VISIBLE);
                if (onChooseListener != null) {
                    onChooseListener.onClick();
                }
                return false;
            }
        });
    }

    @Override
    public void onItemMoved(int fromPosition, int toPosition) {
        Collections.swap(getmSource(), fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        selectPoi = toPosition;
    }

    @Override
    public void onItemDismiss(int position) {
        getmSource().remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public void romeall() {
        getmSource().removeAll(getmSource());
    }

    public class DefaultHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @BindView(R.id.view_line)
        View viewLine;
        @BindView(R.id.img_goods)
        ImageView imgGoods;
        @BindView(R.id.btn_move_up)
        SuperButton btnMoveUp;
        @BindView(R.id.btn_move_down)
        SuperButton btnMoveDown;
        @BindView(R.id.layout_edit)
        LinearLayout layoutEdit;
//        int poi;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            btnMoveUp.setOnClickListener(this);
            btnMoveDown.setOnClickListener(this);
        }

        public ImageView getImgGoods() {
            return imgGoods;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_move_up:
                    if (selectPoi == 0) {
                        return;
                    }

                    Collections.swap(getmSource(), selectPoi, selectPoi - 1);
                    notifyItemMoved(selectPoi, selectPoi - 1);
                    selectPoi = selectPoi - 1;
                    notifyDataSetChanged();
                    break;
                case R.id.btn_move_down:
                    if (getItemCount() >= 2 && selectPoi < getItemCount() - 1) {
                        Collections.swap(getmSource(), selectPoi, selectPoi + 1);
                        selectPoi = selectPoi + 1;
                        notifyItemMoved(selectPoi, selectPoi + 1);
                        selectPoi = selectPoi + 1;
                        notifyDataSetChanged();
                    }
                    break;
            }
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick();
    }

    public void setClearOperator() {
        selectPoi = -1;
        notifyDataSetChanged();
    }
}
